import QtQuick 1.0
import com.nokia.meego 1.0
import ActionMapper 1.0

Page {
    id: root
    tools: commonTools

    function showButtonInput() {
        if (pageStackItem.busy || pageStackItem.currentPage == buttonTab)
            return;
        buttonInputButton.checked = true
        showPage(buttonTab)
    }

    function showMouseInput() {
        if (pageStackItem.busy || pageStackItem.currentPage == touchPadTab)
            return;
        mouseInputButton.checked = true
        showPage(touchPadTab)
    }

    function showTextInput() {
        if (pageStackItem.busy || pageStackItem.currentPage == keyBoardTab)
            return;
        textInputButton.checked = true
        showPage(keyBoardTab)
    }

    PageStack {
        id: pageStackItem

        anchors.top: controlSwitchRow.bottom
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right

        Page {
            id: touchPadTab
            orientationLock: PageOrientation.LockPortrait

            Trackpad {
                id: trackpad
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.top: parent.top
                anchors.bottom: parent.bottom
                anchors.margins: 20
            }
        }

        Page {
            id: keyBoardTab
            orientationLock: PageOrientation.LockLandscape

            VirtualKeyboard {
                id: virtualKeyboard
                anchors.fill: parent

                onKeyEvent: appWindow.rpc.call("qmhrpc.processKey", event.charCodeAt(0))
                onReturnEvent: appWindow.rpc.call("qmhrpc.processKey", Qt.Key_Return)
                onBackspaceEvent: appWindow.rpc.call("qmhrpc.processKey", Qt.Key_Backspace)
            }
        }

        Page {
            id: buttonTab
            orientationLock: PageOrientation.LockPortrait

            property int squareSize: 130

            Button {
                id: buttonUp
                text: qsTr("up")
                onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.Up)
                anchors.bottom: buttonEnter.top
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.margins: 10
                width: buttonTab.squareSize
                height: width
            }

            Button {
                id: buttonDown
                text: qsTr("down")
                onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.Down)
                anchors.top: buttonEnter.bottom
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.margins: 10
                width: buttonTab.squareSize
                height: width
            }

            Button {
                id: buttonLeft
                text: qsTr("left")
                onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.Left)
                anchors.right: buttonEnter.left
                anchors.verticalCenter: buttonEnter.verticalCenter
                anchors.margins: 10
                width: buttonTab.squareSize
                height: width
            }

            Button {
                id: buttonRight
                text: qsTr("right")
                onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.Right)
                anchors.left: buttonEnter.right
                anchors.verticalCenter: buttonEnter.verticalCenter
                anchors.margins: 10
                width: buttonTab.squareSize
                height: width
            }

            Button {
                id: buttonEnter
                text: qsTr("OK")
                onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.Enter)
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                width: buttonTab.squareSize
                height: width
            }

            Button {
                id: buttonMenu
                text: qsTr("Menu")
                onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.Menu)
                anchors.top: parent.top
                anchors.left: parent.left
                anchors.margins: 16
                width: buttonTab.squareSize
                height: width
            }

            Button {
                id: buttonContext
                text: qsTr("Context")
                onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.Context)
                anchors.top: parent.top
                anchors.right: parent.right
                anchors.margins: 16
                width: buttonTab.squareSize
                height: width
            }

            ButtonRow {
                anchors.bottom: parent.bottom
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.margins: 16
                exclusive: false

                Button {
                    width: 25
                    height: width
                    iconSource: "image://theme/icon-m-toolbar-mediacontrol-previous"
                    onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.MediaPrevious)
                }

                Button {
                    width: 25
                    height: width
                    iconSource: "image://theme/icon-m-toolbar-mediacontrol-stop"
                    onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.MediaStop)
                }


                Button {
                    width: 25
                    height: width
                    iconSource: "image://theme/icon-m-toolbar-mediacontrol-play"
                    onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.MediaPlay)
                }


                Button {
                    width: 25
                    height: width
                    iconSource: "image://theme/icon-m-toolbar-mediacontrol-next"
                    onClicked: appWindow.rpc.call("qmhrpc.takeAction", ActionMapper.MediaNext)
                }
            }
        }
    }

    function showPage(page) {
        if (pageStackItem.busy)
            return;

        pageStackItem.replace(page)
    }

    ButtonRow {
        id: controlSwitchRow
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.margins: 16

        Button {
            id: buttonInputButton
            text: qsTr("Buttons")
            onClicked: root.showButtonInput()
        }
        Button {
            id: mouseInputButton
            text: qsTr("TouchPad")
            onClicked: root.showMouseInput()
        }
        Button {
            id: textInputButton
            text: qsTr("Keyboard")
            onClicked: root.showTextInput()
        }
    }

    Component.onCompleted: pageStackItem.replace(buttonTab)
}
